
var vueApp = new Vue({
	el: '#main',
	data : {
		title: '验证好友',
		message: '',
		icon_show: false,
		default: true,
		requestList:[],
		responseList:[],
		groupList: [],
		searchList: [],
		friendList: [],
		remain: false,
		sumlen: 0
	},
	mounted: function() {
		this.init(3);
	},
	methods: {
		init: function(rem) {
			var that = this;
			var requestListItem = util.localGet('control');
			var len = 0;
			for( var i in requestListItem) {
				var obj = requestListItem[i];
				len++;
				var simpleObj = that.transObj(obj);
				if(simpleObj.status == 'REQ')
					that.requestList.push(simpleObj);
				else if(simpleObj.status == 'RESP')
					that.responseList.push(simpleObj);
				else if(simpleObj.status == 'LINK')
					that.friendList.push(simpleObj);

			}

			that.sumlen = len;
			var remain = rem;
			for( var i=0; i<rem; i++) {
				if(that.requestList.length > 0) {
					that.groupList.push(that.requestList.pop());
					remain -- ;
					continue;
				}
				if(that.responseList.length > 0) {
					that.groupList.push(that.responseList.pop());
					remain -- ;
					continue;
				}
				break;
			}
			if( remain <= 0) {
				that.remain = true;
			} else {
				that.remain = false;
			}
		},
		more: function(rem) {
			var that = this;
			var remain = rem;
			for( var i=0; i<rem; i++) {
				if(that.requestList.length > 0) {
					that.groupList.push(that.requestList.pop());
					remain -- ;
					continue;
				}
				if(that.responseList.length > 0) {
					that.groupList.push(that.responseList.pop());
					remain -- ;
					continue;
				}
				break;
			}

			if( remain <= 0 ) {
				that.remain = true;
			} else {
				that.remain = false;
			}
		},
		reset: function(){
			this.icon_show = false
		},
		iconShow: function() {
			if(!this.icon_show)
				this.icon_show = true
			else
				this.icon_show = false
		},
		goto: function(url,e) {
			window.location.replace(url);
		},
		admit: function(obj, e) {
			var that = this;
			util.tokenPost('https://www.maksdu.com/app/communicate/admit',{
				avator: obj.headerUrl,
				fromId: window.localStorage.getItem('userid'),
				requestDate: new Date(),
				nickName: obj.name,
				status: obj.status,
				userId: obj.id,
				username: obj.name
			},function(result) {
				that.groupList.del(function(ids) {
					return ids.id == obj.id;
				})
				var requestListItem = util.localGet('control');
				requestListItem.del(function(ids) {
					return ids.userId == result.userId;
				});
				util.localSet('control', requestListItem);
				obj.status = 'LINK';
				that.friendList.push(obj);
			})
		},
		transObj: function(obj) {
			return {
				id: obj.userId,
				headerUrl: obj.avator != null ? obj.avator: 'images/default.jpg',
				name: obj.username,
				nickName: obj.nickName,
				message: obj.status == 'RESP'?'好友申请还未通过': (obj.status == 'LINK'? '已同意' : '请求添加您为好友'),
				status: obj.status,
				date: new Date(obj.requestDate).Format('HH:mm'),
			};
		}
	}
})